package com.open4cn.simulator.answer.datacolor;

import com.open4cn.simulator.core.answer.AbstractAction;
import com.open4cn.simulator.core.ext.DataColorStatus;

import java.util.List;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-05 17:01
 */
public class BlackboardAction extends AbstractAction {

    public BlackboardAction() {
        super(null);
    }

    public BlackboardAction(AbstractAction action) {
        super(action);
    }

    @Override
    public void action(Object... params) {
        DataColorStatus dataColorStatus = (DataColorStatus) params[0];
        if (!dataColorStatus.inCorrecting()) {
            dataColorStatus.startCorrection();
        }
        dataColorStatus.triggerBlackboard();
        dataColorStatus.changeCorrectionStage(DataColorStatus.CorrectionStageEnum.BLACK_BOARD);

        /**
         * RINR000
         * RENR000
         * RIMR000
         * REMR000
         * RISR000
         * RESR000
         * RIUR000
         * REUR000
         */
        StringBuilder builder = new StringBuilder();
        builder.append("R")
                .append(dataColorStatus.getCurrentSpecularGloss().getCode())
                .append(dataColorStatus.getCurrentAperture().name())
                .append("R000");
        List<String> correctionHistory = dataColorStatus.getCorrectionHistory();
        for (int i = 0, size = correctionHistory.size(); i < size; i++) {
            if (DataColorStatus.EMPTY_CORRECTION_RECORD.equals(correctionHistory.get(i))) {
                correctionHistory.set(i, builder.toString());
                break;
            } else if (i == (size - 1)) {
                correctionHistory.set(0, builder.toString());
            }
        }
        if (hasNext()) {
            action.action(params);
        }
    }
}
